﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLServerHealthCheck.Model;
using System.Data.Sql;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using SQLServerHealthCheck.SQLServer.AutoFixes;
using System.Reflection;
using SQLServerHealthCheck.SQLServer.IssueTypes;
namespace PlayGround
{
    public partial class Form1 : Form
    {
        public List<List<int>> databasenumbers = new List<List<int>>();
        HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"].ToString());
        public Form1()
        {
            InitializeComponent();
        }

        private Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
        {
            return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
        }
        private void btnPlay_Click(object sender, EventArgs e)
        {
            

            ////Type[] typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "SQLServerHealthCheck.SQLServer.IssueTypes");
            //for (int i = 0; i < typelist.Length; i++)
            //{
            //    MessageBox.Show(typelist[i].Name);
            //}
            double testwaarden = 50;
            var qry = from db in dc.Databases
                      from t in db.Tables
                from i in t.Indexes
                      where i.IndexUpdates  > (i.IndexSeeks + i.IndexScans + i.IndexLookups) * ((100d + 50d)/100d)
                      && i.IndexType == "NONCLUSTERED INDEX"
                      select new {DatabaseName = db.DatabaseName , IndexName = i.IndexName , TableName = t.TableName ,seeks =  i.IndexUpdates , bla=  ((i.IndexSeeks + i.IndexScans + i.IndexLookups) * ((100 + 50)/100)) };
            try
            {

                if (qry.Count() != 0)
                {

                    dbgResults.DataSource = qry.ToList();


                }


                else { MessageBox.Show("No Database Records matches the Specified Query"); }
            }
            catch (Exception sqlex)
            {

                MessageBox.Show(sqlex.Message);
            }







        }

    }
}
